package SelectionSort;

import LinearSearch.ArrayGenerator;
import LinearSearch.LinearSearch;
import com.sun.scenario.effect.impl.prism.PrImage;

import java.util.Arrays;

/**
 * ClassName: SelectionSort
 * Package: SelectionSort
 *
 * @Author 马学兴
 * @Create 2023/11/29 0:11
 * @Version 1.0
 * Description:选择排序
 */
public class SelectionSort {
    private SelectionSort(){}
    public static <E extends Comparable<E>> void sort(E[] arr){
        //arr[0..i]是有序的，arr[i..n)是无序的
        for (int i = 0; i < arr.length; i++) {
            int minindex=i;
            for (int j = i; j < arr.length ; j++) {
                if (arr[j].compareTo(arr[minindex])<0)
                    minindex=j;
            }
            swap(arr,i,minindex);
        }
    }
    public static <E extends Comparable> void sort2(E[] arr){
        //arr[0..i]是有序的，arr[i..n)是无序的  循环不变量
        //下面作业：arr[0..i]是无序的，arr[i..n)是有序的  循环不变量
        for (int i = arr.length-1; i>=0 ; i--) {
            int maxindex=i;
            for (int j = i; j >=0 ; j--) {
                if (arr[j].compareTo(arr[maxindex])>0)
                    maxindex=j;
            }
            swap(arr,i,maxindex);
        }
    }
    private static <E>void swap(E[] arr,int i,int j){
        E t=arr[i];
        arr[i]=arr[j];
        arr[j]=t;
    }

    public static void main(String[] args) {
       /* Integer[] arr={4,3,6,7,5};//使用泛型需要使用包装类
        SelectionSort.sort(arr);
        for (int e:arr)
            System.out.println(e+" ");
        System.out.println();

        Student[] students={
                new Student("alick",90),
                new Student("blue",65),
                new Student("an",22),
        };
        SelectionSort.sort(students);
        for (Student student:students)
            System.out.println(student+" ");
        System.out.println();*/

        int[] dataSize={100,10000};
        for (int n:dataSize){
            Integer [] arr= ArrayGenerator.generateRandomArray(n,n);
            Integer [] arr2= Arrays.copyOf(arr,arr.length);
            SortingHelper.sortTest("SelectionSort",arr);
            SortingHelper.sortTest("SelectionSort",arr2);
        }

    }

}
class Student implements Comparable<Student>{
    private String name;
    private int score;
    public Student(String name,int score){
        this.name=name;
        this.score=score;
    }
    @Override
    public int compareTo(Student another) {
      /* if (this.score< another.score)
           return -1;
       else if (this.score==another.score)
           return 0;
       return 1;*/
       return this.score - another.score;
    }
    @Override
    public boolean equals(Object student){//自定义equal方法
        if (this==student)
            return true;
        if (student==null)
            return false;
        if (this.getClass()!=student.getClass())
            return false;
        Student another= (Student) student;
        return this.name.equals(another.name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}